package com.open.platform.oauth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.JdbcApprovalStore;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;

import javax.sql.DataSource;

@Configuration
@EnableAuthorizationServer
public class AuthoriationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    public AuthenticationManager authenticationManager;
    @Autowired
    public TokenStore jwtTokenStore;
    @Autowired
    public TokenEnhancerChain tokenEnhancerChain;
    @Autowired
    public ClientDetailsService jdbcClientDetailsService;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.checkTokenAccess("permitAll()");
        security.tokenKeyAccess("permitAll()");
        security.allowFormAuthenticationForClients();

    }





    /**
     * 使用jdbc数据库的方式保存用户授权批准记录
     * @return
     */
    @Bean
    public JdbcApprovalStore approvalStore(){
        return new JdbcApprovalStore(dataSource);
    }



    /**
     * 数据库保存授权码
     * @return
     */
    @Bean
    public AuthorizationCodeServices authorizationCodeServices(){
        return  new JdbcAuthorizationCodeServices(dataSource);
    }

    /**
     *配置令牌端点和令牌服务
     * /oauth/authorize:授权端点
     * /oauth/confirm_access:用户确认授权提交端点
     * /oauth/token:令牌端点
     * /oauth/check_tokne:令牌校验端点
     * /oauth/token_key:提供公钥端点 jwt使用
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
        endpoints.authorizationCodeServices(authorizationCodeServices());
        endpoints.userDetailsService(userDetailsService);
        endpoints.tokenServices(tokenServices());


    }

    @Bean
    public AuthorizationServerTokenServices tokenServices(){
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setClientDetailsService(jdbcClientDetailsService);
        tokenServices.setTokenStore(jwtTokenStore);
        tokenServices.setTokenEnhancer(tokenEnhancerChain);
        tokenServices.setReuseRefreshToken(true);
        tokenServices.setAccessTokenValiditySeconds(7200);//令牌得有效时间
        tokenServices.setRefreshTokenValiditySeconds(2592000);//刷新令牌默认有效期3天
        return tokenServices;
    }





}
